package ro.ubbcluj.cs.validation.framework.model;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import ro.ubbcluj.cs.validation.framework.core.ParameterName;
import ro.ubbcluj.cs.validation.framework.core.ValidationError;
import ro.ubbcluj.cs.validation.framework.core.ValidationErrorType;

public class BaseValidator {

	protected ValidationError validateRequired(String value, Map<ParameterName, String> validationParameters){
		String required = validationParameters.get(ParameterName.required);
		if (required != null && required.equalsIgnoreCase("true") && StringUtils.isEmpty(value)) {
			return new ValidationError(ValidationErrorType.REQUIRED, null);
		}
		return ValidationError.NO_ERRORS;
	}
	
	protected ValidationError validateMinLength(String value, Map<ParameterName, String> validationParameters){
		String minlength = validationParameters.get(ParameterName.minlength);
		if (minlength != null && StringUtils.isNotEmpty(value) && value.length() < Integer.parseInt(minlength)) {
			return new ValidationError(ValidationErrorType.MIN_LENGTH, new String[]{minlength});
		}
		return ValidationError.NO_ERRORS;
	}
	
	protected ValidationError validateMaxLength(String value, Map<ParameterName, String> validationParameters){
		String maxlength = validationParameters.get(ParameterName.maxlength);
		if (maxlength != null && StringUtils.isNotEmpty(maxlength) && value.length() > Integer.parseInt(maxlength)) {
			return new ValidationError(ValidationErrorType.MAX_LENGTH, new String[]{maxlength});
		}
		return ValidationError.NO_ERRORS;
	}
}
